package com.example.service;

import cn.hutool.core.collection.CollectionUtil;
import com.example.dao.GoodsinfosInfoDao;
import com.example.entity.*;
import com.example.entity.GoodsinfosInfo;
import com.example.exception.CustomException;
import com.example.vo.GoodsinfosInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class GoodsinfosInfoService {

    @Resource
    private GoodsinfosInfoDao goodsinfosInfoDao;
	
	
	//kuabiaojisuan3

    public GoodsinfosInfo add(GoodsinfosInfo goodsinfosInfo) {
        List<Long> picflst = goodsinfosInfo.getPicflst();
        if (!CollectionUtil.isEmpty(picflst)) {
            goodsinfosInfo.setPic(picflst.toString());
        }
		
		        //shangxchxuantupxian
        // 唯一校验
        int count = goodsinfosInfoDao.checkRepeat("goodsnumbers", goodsinfosInfo.getGoodsnumbers(), null);
if (count > 0) {
	throw new CustomException("1001", "该商品编号\"" + goodsinfosInfo.getGoodsnumbers() + "\"已存在");
}
      // goodsinfosInfo.setAccount((double) 0);
	  
        goodsinfosInfoDao.insertSelective(goodsinfosInfo);
        return goodsinfosInfo;
    }
	
	public List<Map<String,String>> findByDiqu() {
        return goodsinfosInfoDao.findByDiqu();
    }

    public void delete(Long id) {
        goodsinfosInfoDao.deleteByPrimaryKey(id);
    }

    public void update(GoodsinfosInfo goodsinfosInfo) {
        List<Long> picflst = goodsinfosInfo.getPicflst();
        if (!CollectionUtil.isEmpty(picflst)) {
            goodsinfosInfo.setPic(picflst.toString());
        }
		
		        //shangxchxuantupxian
		//youdianzan
        goodsinfosInfoDao.updateByPrimaryKeySelective(goodsinfosInfo);
    }

    public GoodsinfosInfo findById(Long id) {
        return goodsinfosInfoDao.selectByPrimaryKey(id);
    }
	
    public List<GoodsinfosInfoVo> findAll() {
        return goodsinfosInfoDao.findByGoodsnumbers("all");
    }

    public PageInfo<GoodsinfosInfoVo> findPage(String goodsnumbers, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<GoodsinfosInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        if (user.getLevel().equals("管理员")) {all = goodsinfosInfoDao.findByGoodsnumbers(goodsnumbers);}
		
        
        return PageInfo.of(all);
    }
	
	public PageInfo<GoodsinfosInfoVo> findPageqt(String goodsnumbers, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<GoodsinfosInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        all = goodsinfosInfoDao.findByGoodsnumbers(goodsnumbers);
		
	
	
        
        return PageInfo.of(all);
    }

   // public GoodsinfosInfoVo findByUserName(String name) {
//        return goodsinfosInfoDao.findByUsername(name);
//    }

	//yoxulogindenxglu
	
    public void changeStatus(Long id) {
        GoodsinfosInfo goodsinfosInfo = goodsinfosInfoDao.selectByPrimaryKey(id);
        if(goodsinfosInfo.getStatus().equals("是")){
            goodsinfosInfo.setStatus("否");
            goodsinfosInfoDao.updateByPrimaryKey(goodsinfosInfo);
        }else if(goodsinfosInfo.getStatus().equals("否")){
            goodsinfosInfo.setStatus("是");
            goodsinfosInfoDao.updateByPrimaryKey(goodsinfosInfo);
        }
    }
	//ddaizdhifu
	public Integer tixing() { return goodsinfosInfoDao.tixing(); }

public List<GoodsinfosInfoVo> findByType(String type) {
        return goodsinfosInfoDao.findByType(type);
}

    public List<GoodsinfosInfo> searchGoods(String text) {
        return goodsinfosInfoDao.searchGoods(text);
    }


    //xiugaimimazys
	
}
